package com.jsnkj.basic.util

import android.content.Context
import android.content.SharedPreferences
import android.provider.Settings
import com.jsnkj.basic.AdLibApplication
import com.jsnkj.basic.bean.AdPostTypeBean
import com.jsnkj.basic.bean.AdPostTypeListBean
import com.jsnkj.basic.bean.AdSettingBaseEntity
import com.jsnkj.basic.bean.InvitesBean
import com.jsnkj.basic.bean.MyInformationBean
import com.jsnkj.basic.bean.SettingEntity
import com.jsnkj.basic.bean.UserDataEntity
import com.jsnkj.basic.config.RewardsConfig
import com.jsnkj.basic.config.SpConfig

class SharePreferencesUtil {

    companion object {
        private var sInstance: SharePreferencesUtil? = null

        private const val SHARED_NAME = "video_advert_sing"

        fun getInstance(): SharePreferencesUtil {
            if (sInstance == null) {
                synchronized(SharePreferencesUtil::class.java) {
                    if (sInstance == null) {
                        sInstance = SharePreferencesUtil()
                    }
                }
            }

            return sInstance!!
        }
    }

    private var sharedReadable: SharedPreferences? = null
    private var sharedWritable: SharedPreferences.Editor? = null

    constructor() {
        sharedReadable = AdLibApplication.getInstance()
            .getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE)
        sharedWritable = sharedReadable!!.edit()
    }


    fun getString(key: String?): String? {
        return sharedReadable!!.getString(key, "")
    }

    fun putString(key: String?, value: String?) {
        sharedWritable!!.putString(key, value)
        sharedWritable!!.commit()
    }

    fun putInt(key: String?, value: Int) {
        sharedWritable!!.putInt(key, value)
        sharedWritable!!.commit()
    }

    fun putBoolean(key: String?, value: Boolean) {
        sharedWritable!!.putBoolean(key, value)
        sharedWritable!!.commit()
    }

    fun getInt(key: String?, def: Int): Int {
        return sharedReadable!!.getInt(key, def)
    }


    fun getBoolean(key: String?, def: Boolean): Boolean {
        return sharedReadable!!.getBoolean(key, def)
    }

    fun putLong(key: String?, value: Long?) {
        sharedWritable!!.putLong(key, value!!)
        sharedWritable!!.commit()
    }

    fun getLong(key: String?, def: Long?): Long {
        return sharedReadable!!.getLong(key, def!!)
    }


    fun getUid(): String {
        var uid = ""
        getUserBean()?.let {
            it.user?.let { user ->
                uid = user.uuid.toString()
            }
        }

        if (uid.isNullOrEmpty()) {
            return Settings.System.getString(
                AdLibApplication.getInstance().getContentResolver(),
                Settings.System.ANDROID_ID
            )
        }
        return uid
    }


    /**
     * 得到开屏广告的请求时间
     *
     * @return
     */
    fun getStreamOpenADTime(): Long {
        return getInstance().getLong(SpConfig.STREAM_OPEN_AD_TIME, 0L)
    }


    fun putStreamOpenADTime(time: Long?) {
        getInstance().putLong(SpConfig.STREAM_OPEN_AD_TIME, time)
    }


    fun getIsFirstInit(): Boolean {
        return getInstance().getBoolean(SpConfig.IS_FIRST_INIT, false)
    }

    fun setIsFirstInit(isFirstInit: Boolean) {
        getInstance().putBoolean(SpConfig.IS_FIRST_INIT, isFirstInit)
    }


    /**
     * 得到请求配置数据
     *
     * @return
     */
    fun getSettingEntity(): SettingEntity {
        val beanStr = getInstance().getString(SpConfig.APP_SETTING_ENTITY)
        com.jsnkj.basic.util.LogUtils.i("配置数据："+beanStr);
        if (beanStr.isNullOrEmpty()) {
            val entity = SettingEntity()
            entity.invites = InvitesBean()
            return entity
        }
        return beanStr.jsonToClass<SettingEntity>()
    }


    fun putSettingEntity(bean: SettingEntity) {
        getInstance().putString(SpConfig.APP_SETTING_ENTITY, bean.jsonToString())
    }


    /**
     * 得到广告数据
     *
     * @return
     */
    fun getAdSettingEntity(): AdSettingBaseEntity {
        val beanStr = getInstance().getString(SpConfig.AD_SETTING_ENTITY)
        if (beanStr.isNullOrEmpty()) {
            return AdSettingBaseEntity()
        }
        return beanStr.jsonToClass<AdSettingBaseEntity>()
    }


    fun putAdSettingEntity(bean: AdSettingBaseEntity) {
        getInstance().putString(SpConfig.AD_SETTING_ENTITY, bean.jsonToString())
    }


    /**
     *  保存广告展示的信息
     *
     * @return
     */
    fun putAdPostTypeBean(typeBean: AdPostTypeBean) {
        val bean = getAdPostTypeBean()
        bean.list.add(typeBean)
        sharedWritable!!.putString(RewardsConfig.adPostTypeBean, bean.jsonToString())
        sharedWritable!!.commit()
    }


    /**
     * 清除广告数据
     */
    fun clearAdBeanList() {
        var beanCacheList: ArrayList<AdPostTypeBean> = ArrayList()
        beanCacheList.addAll(getInstance().getAdPostTypeBean().list)
        val list = getInstance().getAdPostTypeBean().list.filter { adPostTypeBean ->
            !RewardsConfig.beanList.any { it.equals(adPostTypeBean) }
        }

        val bean = getAdPostTypeBean()
        bean.list = list as ArrayList<AdPostTypeBean>
        sharedWritable!!.putString(RewardsConfig.adPostTypeBean, bean.jsonToString())
        sharedWritable!!.commit()
    }

    fun clearAllAdBeanList() {
        sharedWritable!!.putString(RewardsConfig.adPostTypeBean, "")
        sharedWritable!!.commit()
    }

    /**
     *  得到广告展示的信息
     */
    fun getAdPostTypeBean(): AdPostTypeListBean {

        val bean = sharedReadable!!.getString(RewardsConfig.adPostTypeBean, "")
        if (bean.isNullOrEmpty()) {
            return AdPostTypeListBean()
        }
        return bean.jsonToClass<AdPostTypeListBean>()
    }


    /**
     *  保存会员信息
     *
     * @return
     */
    fun putUserBean(bean: UserDataEntity?) {
        if (null == bean) {
            sharedWritable!!.putString(SpConfig.USER_LOGIN_ENTITY, "")
        } else {
            sharedWritable!!.putString(SpConfig.USER_LOGIN_ENTITY, bean.jsonToString())
        }
        sharedWritable!!.commit()
    }

    fun getUserBean(): UserDataEntity? {

        val bean = sharedReadable!!.getString(SpConfig.USER_LOGIN_ENTITY, "")
        if (bean.isNullOrEmpty()) {
            return null
        }
        return bean.jsonToClass<UserDataEntity>()
    }



    /**
     * 得到广告数据
     *
     * @return
     */
    fun getMyInformationBean(): MyInformationBean {
        val beanStr = getInstance().getString(SpConfig.APP_MY_INFOMATION)
        if (beanStr.isNullOrEmpty()) {
            return MyInformationBean()
        }
        return beanStr.jsonToClass<MyInformationBean>()
    }


    fun putMyInformationBean(bean: MyInformationBean) {
        getInstance().putString(SpConfig.APP_MY_INFOMATION, bean.jsonToString())
    }


}










